<head>
    <meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">

<script id="shader-fs" type="x-shader/x-fragment">
    precision mediump float;

    void main(void) {
        gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
    }
</script>

<script id="shader-vs" type="x-shader/x-vertex">
    attribute vec3 aVertexPosition;

    uniform mat4 uMVMatrix;
    uniform mat4 uPMatrix;

    void main(void) {
        gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
    }
</script>


<script type="text/javascript">
var gl;
var mvMatrix, pMatrix;

function initGL(canvas) {
	try {
            gl = canvas.getContext("experimental-webgl");
            gl.viewportWidth = canvas.width;
            gl.viewportHeight = canvas.height;
        } catch (e) {
        }
        if (!gl) {
            alert("Could not initialise WebGL, sorry :-(");
        }
}

function identityMatrix() {
   	var buf = new ArrayBuffer(Float32Array.BYTES_PER_ELEMENT*16);
	var view32f = new Float32Array(buf); 
	view32f[0] = 1; 
	view32f[1] = 0; 
	view32f[2] = 0; 
	view32f[3] = 0;

	view32f[4] = 0; 
	view32f[5] = 1; 
	view32f[6] = 0; 
	view32f[7] = 0;

	view32f[8] = 0; 
	view32f[9] = 0; 
	view32f[10] = 1; 
	view32f[11] = 0;

	view32f[12] = 0; 
	view32f[13] = 0; 
	view32f[14] = 0; 
	view32f[15] = 1;

	return view32f;
}

function translationFromView(v, tx, ty, tz) {

	v[12] = tx;
	v[13] = ty;
	v[14] = tz;
    	return v;
}

function projectionMatrix(left, right, bottom, top,near, far)
{
	var x,y,a,b,c,d;
	x = 2.0*near / (right - left);
	y = 2.0*near / (top - bottom);
	a = (right + left) / (right - left);
	b = (top + bottom) / (top - bottom);
	c = -(far + near) / (far - near);
	d = -(2.0*far*near) / (far - near);

	var buf = new ArrayBuffer(Float32Array.BYTES_PER_ELEMENT*16);
	var view32f = new Float32Array(buf); 
	view32f[0] = x; 
	view32f[4] = 0; 
	view32f[8] = a; 
	view32f[12] = 0;

	view32f[0 + 1] = 0; 
	view32f[4 + 1] = y; 
	view32f[8 + 1] = b; 
	view32f[12 + 1] = 0;

	view32f[0+2] = 0; 
	view32f[4+2] = 0; 
	view32f[8+2] = c; 
	view32f[12+2] = d;

	view32f[0+3] = 0; 
	view32f[4+3] = 0; 
	view32f[8+3] = -1; 
	view32f[12+3] = 0;

	return view32f;
}

function getShader(gl, id) {
        var shaderScript = document.getElementById(id);
        if (!shaderScript) {
            return null;
        }

        var str = "";
        var k = shaderScript.firstChild;
        while (k) {
            if (k.nodeType == 3) {
                str += k.textContent;
            }
            k = k.nextSibling;
        }

        var shader;
        if (shaderScript.type == "x-shader/x-fragment") {
            shader = gl.createShader(gl.FRAGMENT_SHADER);
        } else if (shaderScript.type == "x-shader/x-vertex") {
            shader = gl.createShader(gl.VERTEX_SHADER);
        } else {
            return null;
        }

        gl.shaderSource(shader, str);
        gl.compileShader(shader);

        if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) {
            alert(gl.getShaderInfoLog(shader));
            return null;
        }

        return shader;
    }


    var shaderProgram;

    function initShaders() {
        var fragmentShader = getShader(gl, "shader-fs");
        var vertexShader = getShader(gl, "shader-vs");

        shaderProgram = gl.createProgram();
        gl.attachShader(shaderProgram, vertexShader);
        gl.attachShader(shaderProgram, fragmentShader);
        gl.linkProgram(shaderProgram);

        if (!gl.getProgramParameter(shaderProgram, gl.LINK_STATUS)) {
            alert("Could not initialise shaders");
        }

        gl.useProgram(shaderProgram);

        shaderProgram.vertexPositionAttribute = gl.getAttribLocation(shaderProgram, "aVertexPosition");
        gl.enableVertexAttribArray(shaderProgram.vertexPositionAttribute);

        shaderProgram.pMatrixUniform = gl.getUniformLocation(shaderProgram, "uPMatrix");
        shaderProgram.mvMatrixUniform = gl.getUniformLocation(shaderProgram, "uMVMatrix");
    }




	function InitMatrices() {
		mvMatrix = translationFromView(identityMatrix(), -1.5, 0.0, -1.0);
		var aspect = gl.viewportWidth / gl.viewportHeight;
		pMatrix = projectionMatrix(-aspect*0.5, aspect*0.5, -.5, .5, .1, 1000.0);
	}

    function setMatrixUniforms() {
        gl.uniformMatrix4fv(shaderProgram.pMatrixUniform, false, pMatrix);
        gl.uniformMatrix4fv(shaderProgram.mvMatrixUniform, false, mvMatrix);
    }


    var triangleVertexPositionBuffer;
    var squareVertexPositionBuffer;

    function initBuffers() {
        triangleVertexPositionBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
        var vertices = [
             0.0,  1.0,  0.0,
            -1.0, -1.0,  0.0,
             1.0, -1.0,  0.0
        ];
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
        triangleVertexPositionBuffer.itemSize = 3;
        triangleVertexPositionBuffer.numItems = 3;

        squareVertexPositionBuffer = gl.createBuffer();
        gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
        vertices = [
             1.0,  1.0,  0.0,
            -1.0,  1.0,  0.0,
             1.0, -1.0,  0.0,
            -1.0, -1.0,  0.0
        ];
        gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(vertices), gl.STATIC_DRAW);
        squareVertexPositionBuffer.itemSize = 3;
        squareVertexPositionBuffer.numItems = 4;
    }


    function drawScene() {
        gl.viewport(0, 0, gl.viewportWidth, gl.viewportHeight);
        gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);

        //mat4.perspective(45, gl.viewportWidth / gl.viewportHeight, 0.1, 100.0, pMatrix);
	//mat4.identity(mvMatrix);
	//mat4.translate(mvMatrix, [-1.5, 0.0, -7.0]);

	mvMatrix[12] = -1.5;

        gl.bindBuffer(gl.ARRAY_BUFFER, triangleVertexPositionBuffer);
        gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, triangleVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
        setMatrixUniforms();
        gl.drawArrays(gl.TRIANGLES, 0, triangleVertexPositionBuffer.numItems);

	//mat4.translate(mvMatrix, [3.0, 0.0, 0.0]);
	mvMatrix[12] = 1.5;

        gl.bindBuffer(gl.ARRAY_BUFFER, squareVertexPositionBuffer);
        gl.vertexAttribPointer(shaderProgram.vertexPositionAttribute, squareVertexPositionBuffer.itemSize, gl.FLOAT, false, 0, 0);
        setMatrixUniforms();
        gl.drawArrays(gl.TRIANGLE_STRIP, 0, squareVertexPositionBuffer.numItems);
    }



    function webGLStart() {
        var canvas = document.getElementById("xmas-canvas");
	initGL(canvas);
	InitMatrices();
        initShaders();
        initBuffers();

        gl.clearColor(0.0, 0.0, 0.0, 1.0);
        gl.enable(gl.DEPTH_TEST);

        drawScene();
    }

</script>

</head>

<body onload="webGLStart();">
    <canvas id="xmas-canvas" style="border: 1px;" width="320" height="200">
    </canvas>

</body>

